//
//  NSString+DisplayTime.m
//  lc-collect
//
//  Created by mac on 2021/3/3.
//

#import "NSString+DisplayTime.h"

@implementation NSString (DisplayTime)

/// 转化为时分秒
/// @param seconds 多少秒
+ (NSString *)timeformatFromSeconds:(int)seconds {
    
    //format of hour
    int hour = seconds / 3600;
    NSString *str_hour = @"";
    if (hour > 0) {
        str_hour = [NSString stringWithFormat:@"%02d:", seconds / 3600];
    }
    
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02d", (seconds % 3600) / 60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02d", seconds % 60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@", str_hour, str_minute, str_second];
    return format_time;
}

/// 通过时间戳计算时间差（几小时前、几天前）
/// @param compareDate compareDate description
+ (NSString *)compareCurrentTime:(NSTimeInterval)compareDate {
   
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:compareDate/1000];
    
    NSTimeInterval timeInterval = [confromTimesp timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *referenceComponents = [calendar components:unitFlags fromDate:confromTimesp];
    //    NSInteger referenceYear = referenceComponents.year;
    //    NSInteger referenceMonth = referenceComponents.month;
    //    NSInteger referenceDay = referenceComponents.day;
    NSInteger referenceHour = referenceComponents.hour;
    //    NSInteger referemceMinute = referenceComponents.minute;
    
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    } else if((temp= timeInterval / 60) < 60) {
        result = [NSString stringWithFormat:@"%ld分钟前", temp];
    } else if((temp = timeInterval / 3600) < 24) {
        result = [NSString stringWithFormat:@"%ld小时前", temp];
    } else if ((temp = timeInterval / 3600 / 24) == 1) {
        result = [NSString stringWithFormat:@"昨天%ld时", (long)referenceHour];
    } else if ((temp = timeInterval / 3600 / 24) == 2) {
        result = [NSString stringWithFormat:@"前天%ld时", (long)referenceHour];
    } else if((temp = timeInterval / 3600 / 24) < 31){
        result = [NSString stringWithFormat:@"%ld天前", temp];
    } else if((temp = timeInterval / 3600 / 24 / 30) < 12){
        result = [NSString stringWithFormat:@"%ld个月前", temp];
    } else {
        temp = temp / 12;
        result = [NSString stringWithFormat:@"%ld年前", temp];
    }
    
    return  result;
}

/// 通过时间戳得出对应的时间
/// @param timestamp 时间戳
+ (NSString *)getDateStringWithTimestamp:(NSTimeInterval)timestamp {
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents * referenceComponents = [calendar components:unitFlags fromDate:confromTimesp];
    NSInteger referenceYear = referenceComponents.year;
    NSInteger referenceMonth = referenceComponents.month;
    NSInteger referenceDay = referenceComponents.day;
    
    return [NSString stringWithFormat:@"%ld年%ld月%ld日", referenceYear, (long)referenceMonth, (long)referenceDay];
}

/// 通过时间戳和显示时间
/// @param timestamp 时间戳
/// @param formatter 格式
+ (NSString *)getStringWithTimestamp:(NSTimeInterval)timestamp
                            formatter:(NSString*)formatter {
    
    if ([NSString stringWithFormat:@"%@", @(timestamp)].length == 13) {
        timestamp /= 1000.0f;
    }
    NSDate *timestampDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSString *strDate = [dateFormatter stringFromDate:timestampDate];
    
    return strDate;
}

@end
